import { ElTag } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { reactive, h, ref, computed } from 'vue'
import { CrudSchema } from '@/hooks/web/useCrudSchemas'
import { facOutLedgerType, ProducerLedgerType } from '@/api/equip/type'
import { useEquipStore } from '@/store/modules/equip'
const { t } = useI18n()
const EquipStore = useEquipStore()
if (EquipStore.getProducerLedger.length == 0) {
  EquipStore.setProducerLedger()
} //获取厂商列表
//厂商列表
const ProducerList = computed(() => {
  return EquipStore.getProducerLedger
})
export function FunHooks() {
  const saveLoading = ref(false)
  const dialogVisible = ref(false)
  const dialogTitle = ref('添加')
  const actionType = ref('')
  const FormDisabled = ref(false)
  const writeRef = ref()
  const searchParams = ref({})
  const currentRow = ref<facOutLedgerType | null>(null)
  //表格操作
  const action = (row: facOutLedgerType, type: string) => {
    actionType.value = type
    dialogVisible.value = true
    if (type == 'add') {
      currentRow.value = {}
      dialogTitle.value = '添加'
      FormDisabled.value = false
    } else if (type == 'edit') {
      dialogTitle.value = '修改'
      currentRow.value = row
      FormDisabled.value = false
    } else {
      dialogTitle.value = '详情'
      FormDisabled.value = true
      currentRow.value = row
    }
  }
  const delLoading = ref(false)
  const ids = ref<string[]>([])
  const crudSchemas = reactive<CrudSchema[]>([
    {
      field: 'selection',
      type: 'selection',
      fixed: 'left',
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      detail: {
        hidden: true
      }
    },
    {
      field: 'index',
      label: t('tableDemo.index'),
      type: 'index',
      fixed: 'left',
      form: {
        hidden: true
      },
      search: {
        hidden: true
      },
      detail: {
        hidden: true
      }
    },
    {
      field: 'goodsName',
      label: t('设施物料名称'),
      width: 180,
      search: {
        hidden: false,
        component: 'Input',
        componentProps: {
          placeholder: '物料名称'
        }
      },
      form: {
        hidden: false,
        colProps: {
          span: 20
        }
      },
      detail: {
        span: 24
      }
    },
    {
      field: 'state',
      label: t('事件状态'),
      formatter: (_: Recordable, __: any, cellValue: number) => {
        return h(
          ElTag,
          {
            type: cellValue == 0 ? 'success' : 'danger'
          },
          () => (cellValue == 0 ? t('在用') : t('封存'))
        )
      },
      search: {
        hidden: false,
        component: 'Select',
        componentProps: {
          placeholder: '事件状态',
          options: [
            {
              label: '在用',
              value: 0
            },
            {
              label: '封存',
              value: 1
            }
          ]
        }
      },
      form: {
        component: 'RadioGroup',
        componentProps: {
          options: [
            {
              label: '在用',
              value: 0
            },
            {
              label: '封存',
              value: 1
            }
          ]
        },
        colProps: {
          span: 20
        }
      }
    },

    {
      field: 'goodsCode',
      label: t('存货编码'),
      form: {
        hidden: false,
        colProps: {
          span: 20
        }
      },
      search: {
        hidden: true
      },
      detail: {
        span: 24
      }
    },
    {
      field: 'specModel',
      label: t('规格型号'),
      search: {
        hidden: true
      },
      form: {
        hidden: false,
        colProps: {
          span: 20
        }
      },
      width: 180
    },
    {
      field: 'facOutNo',
      label: t('出场编码'),
      search: {
        hidden: true
      },
      width: 180,
      form: {
        hidden: true
      },
      detail: {
        hidden: true
      }
    },
    {
      field: 'isType',
      label: t('类型'),
      formatter: (_: Recordable, __: any, cellValue: number) => {
        return cellValue == 0 ? '设施出厂' : '设备出厂'
      },
      search: {
        hidden: true
      },
      form: {
        component: 'RadioGroup',
        componentProps: {
          options: [
            {
              label: '设施出厂',
              value: 0
            },
            {
              label: '设备出厂',
              value: 1
            }
          ]
        },
        colProps: {
          span: 20
        }
      },
      detail: {
        hidden: true
      }
    },
    {
      field: 'unit',
      label: t('计量单位'),
      form: {
        hidden: false,
        component: 'Input',
        colProps: {
          span: 20
        }
      },
      search: {
        hidden: true
      },
      detail: {
        span: 24
      }
    },
    {
      field: 'producerUuid',
      label: t('厂商'),
      formatter: (_: Recordable, __: any, cellValue: string) => {
        return ProducerList.value.map((item) => {
          if (item.uuid == cellValue) {
            return item.producerName
          }
        })
      },
      form: {
        hidden: false,
        component: 'Select',
        componentProps: {
          options: ProducerList.value.map((item: ProducerLedgerType) => {
            return {
              label: item.producerName,
              value: item.uuid
            }
          })
        },
        colProps: {
          span: 20
        }
      },
      detail: {
        span: 24
      },
      search: {
        hidden: true
      }
    },
    {
      field: 'remark',
      label: t('备注说明'),
      width: 150,
      search: {
        hidden: true
      },
      form: {
        hidden: false,
        component: 'Input',
        colProps: {
          span: 20
        }
      },
      detail: {
        span: 24
      }
    }
  ])
  return {
    saveLoading,
    crudSchemas,
    dialogVisible,
    dialogTitle,
    actionType,
    FormDisabled,
    writeRef,
    searchParams,
    currentRow,
    action,
    delLoading,
    ids
  }
}
